def greatgrandfather(child: str, names: dict) -> str:
    return names[names[names[child]]]

names = {"Иван": "Пётр",
        "Алексей":"Антон",
        "Пётр":"Сергей",
        "Владимер":"Алексей",
        "Сергей":"Борис",
        "Роман":"Владимер"}

assert greatgrandfather("Иван", names) == "Борис"
assert greatgrandfather("Роман", names) == "Антон"

# =================
names = {"Иван": "Пётр",
        "Алексей":"Антон",
        "Пётр":"Сергей",
        "Владимер":"Алексей",
        "Сергей":"Борис",
        "Роман":"Владимер"} 
print(names[names[names["Иван"]]])

